'use strict'
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ? true : false
var Player = {
  isJump: function (name, pv) {
    var url = ''
    switch (name) {
      case 'youku':
        url = this.youku(pv)
        break
      case 'tudou':
        url = this.tudou(pv)
        break
      case 'iqiyi':
        url = this.iqiyi(pv)
        break
      case 'viqiyi':
        url = this.iqiyi(pv)
        break
      case 'letv':
        url = this.letv(pv)
        break
      case 'sohu':
        url = this.sohu(pv)
        break
      case 'pptv':
        url = this.pptv(pv)
        break
      case 'qq':
        url = this.qq(pv)
        break
      case 'bilibili':
        url = this.bilibili(pv)
        break
      case 'acfun':
        url = this.acfun(pv)
        break
      default:
        url = '/'
        break
    }
    return url
  },
  tudou: function (pv) {
    var data = pv.split(',')
    var len = data.length
    if (len === 1) {
      return this.youku(pv)
    } else if (len === 2) {
      return '/'
    } else if (len >= 3) {
      return this.youku(data[2])
    }
  },
  youku: function (pv) {
    var data = pv.split(',')
    var vid = data.length === 3 ? data[2] : pv
    return 'https://v.youku.com/v_show/id_' + vid + '.html'
  },
  iqiyi: function (pv) {
    var plus = isMobile ? '&tvid=' : '&tvId='
    var data = pv.split(/,|&tvid=|_/)
    var vid = pv.indexOf(',') !== -1 || pv.indexOf('_') !== -1 ? data[1] + plus + data[0] : data[0] + plus + data[1]
    return data.length > 1 ? `https://m.iqiyi.com/shareplay.html?vid=${vid}&autoplay=1&fullscreen=1` : '/'
  },
  letv: function (pv) {
    var data = pv.split(',')
    return data.length === 2 ? '/' : 'https://www.le.com/ptv/vplay/' + data[0] + '.html'
  },
  sohu: function (pv) {
    return `https://tv.sohu.com/s/sohuplayer/iplay.html?vid=${pv.split('_')[0]}&autoplay=true`
  },
  pptv: function (pv) {
    return '//' + (isMobile ? 'm' : 'v') + '.pptv.com/show/' + pv.split(',')[0] + '.html'
  },
  qq: function (pv) {
    return `https://v.qq.com/txp/iframe/player.html?vid=${pv}`
  },
  bilibili: function (pv) {
    var data = pv.split(',')
    return pv.indexOf('http') !== -1 ? pv : data.length === 2 ? '//www.bilibili.com/video/av' + data[0] + '/?p=' + data[1] : '//www.bilibili.com/video/av' + pv + '/'
  },
  acfun: function (pv) {
    var vid = ''
    var data = []
    if (pv.indexOf('ab') != -1) {
      var ab = pv.split('ab')[1].split(',')
      vid = ab.length === 2 ? ab[0] + '_' + ab[1] : data[1]
    } else {
      data = pv.split(',')
      vid = data.length === 2 ? data[0] + '_' + data[1] : pv
    }
    return isMobile ? '//m.acfun.cn/v/?' + (pv.indexOf('ab') != -1 ? 'ab' : 'ac') + '=' + vid : '//www.acfun.cn/v/' + (pv.indexOf('ab') != -1 ? 'ab' : 'ac') + vid
  },
  Play: function (name, vid) {
    if (vid.indexOf('@@') !== -1) {
      var data = vid.split('@@')
      name = data[1]
      vid = data[0]
    }
    var playStyle = /acku|sina|letvsaas|weibo|miaopai|letvyun|bit|qqq|yunpan|360|ksyun|s360/.test(name),
      url = ''
    if (/.mp4/.test(vid) || playStyle || /.m3u8/.test(vid)) {
      url = '/'
    } else if (/ikanfan|acgnz/.test(vid)) {
      url = vid.split('=')[1].split('&')[0]
    } else if (/.html|.shtml|.htm|https:\/\/|http:\/\//.test(vid) || name === 'full') {
      url = vid
    } else if (/bilibili|acfun|youku|tudou|iqiyi|pptv|letv|qq|sohu|viqiyi/.test(name)) {
      url = this.isJump(name, vid)
    } else {
      url = vid
    }
    return url
  },
}

export default (name, vid) => {
  if (vid.indexOf('@@') !== -1) {
    const data = vid.split('@@')
    name = data[1]
    vid = data[0]
  }
  return Player.Play(name, vid)
}
